package com.ui.mine.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.BaseRefreshFragment;
import com.bean.LoginBean;
import com.ddd.wandemo.R;
import com.event.LoginEvent;
import com.google.gson.Gson;
import com.ui.mine.presenter.LoginFragmentContract;
import com.ui.mine.presenter.LoginFragmentPresenter;
import com.util.SpUtil;
import com.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class LoginFragment extends BaseRefreshFragment<LoginFragmentPresenter> implements LoginFragmentContract.View {
    private EditText etAccount, etPassword, etRePassword;
    private TextView tvLogin, tvLoginTitle, tvChangeRegiter;
    private String account, pwd, repwd;
    private ImageView ivShowPwd, ivShowRePwd;
    private LinearLayout llRePassword;
    private boolean showPwd = false;
    private boolean showRePwd = false;
    private int type;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login;
    }

    @Override
    protected void initView() {
        etAccount = view.findViewById(R.id.et_login_account);
        etPassword = view.findViewById(R.id.et_login_pwd);
        etRePassword = view.findViewById(R.id.et_login_repwd);
        tvLogin = view.findViewById(R.id.tv_do_login);
        tvLoginTitle = view.findViewById(R.id.tv_login_title);
        tvChangeRegiter = view.findViewById(R.id.tv_change_register);
        ivShowPwd = view.findViewById(R.id.iv_show_pwd);
        ivShowRePwd = view.findViewById(R.id.iv_show_repwd);
        llRePassword = view.findViewById(R.id.ll_re_password);

        if (getArguments() != null) {
            type = getArguments().getInt("type", 0);
            if (type == LoginAndRegisterActivity.REGISTER) {
                llRePassword.setVisibility(View.VISIBLE);
                tvLogin.setText("注册");
                tvChangeRegiter.setText(R.string.login_by_phone);
                tvLoginTitle.setText("账号注册");
            }
        }
    }

    @Override
    protected void initEventAndData() {
        delayLoad = false;
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                account = etAccount.getText().toString().trim();
                pwd = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(account) & TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入账号跟密码");
                } else {
                    if (type == LoginAndRegisterActivity.LOGIN) {
                        mPresenter.doLogin(account, pwd);
                    } else {
                        repwd = etRePassword.getText().toString().trim();
                        mPresenter.doRegister(account, pwd, repwd);
                    }
                }
            }
        });

        //edittext输入监听
        etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 0) {
                    ivShowPwd.setVisibility(View.VISIBLE);
                } else {
                    ivShowPwd.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        //edittext输入监听
        etRePassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 0) {
                    ivShowRePwd.setVisibility(View.VISIBLE);
                } else {
                    ivShowRePwd.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });


        ivShowPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPwd = !showPwd;
                if (showPwd) {
                    etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                ivShowPwd.setSelected(showPwd);
            }
        });

        ivShowRePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showRePwd = !showRePwd;
                if (showRePwd) {
                    etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                ivShowRePwd.setSelected(showRePwd);
            }
        });

        tvChangeRegiter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginFragment loginFragment = new LoginFragment();
                Bundle args = new Bundle();
                switch (type) {
                    case LoginAndRegisterActivity.LOGIN:
                        args.putInt("type", LoginAndRegisterActivity.REGISTER);
                        break;
                    case LoginAndRegisterActivity.REGISTER:
                        args.putInt("type", LoginAndRegisterActivity.LOGIN);
                        break;
                    default:
                        break;
                }
                loginFragment.setArguments(args);
                getFragmentManager().beginTransaction()
                        .replace(R.id.fl_contain,loginFragment)
                        .addToBackStack(null)
                        .commit();
            }
        });
    }

    @Override
    public void onLogin(LoginBean bean) {
        loginSuccess(bean);
    }

    private void loginSuccess(LoginBean bean) {
        //登录成功，发送信息到mineFragment
        Gson gson = new Gson();
        String loginBean = gson.toJson(bean);

        String userEncode = Base64.encodeToString(loginBean.getBytes(), Base64.NO_WRAP);

        SpUtil.setString("userInfo",userEncode);

        ToastUtil.showToast("登录成功");
        EventBus.getDefault().post(new LoginEvent(bean.getUsername()));
        getActivity().finish();
    }

    @Override
    public void onRegister(LoginBean bean) {
        loginSuccess(bean);
    }

    @Override
    public void onLogout() {

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected LoginFragmentPresenter initPresenter() {
        return new LoginFragmentPresenter();
    }
}
